
--region Drop Existing Procedures

IF OBJECT_ID(N'[dbo].[UnderOverPredicts_Insert]') IS NOT NULL
	DROP PROCEDURE [dbo].[UnderOverPredicts_Insert]

IF OBJECT_ID(N'[dbo].[UnderOverPredicts_Update]') IS NOT NULL
	DROP PROCEDURE [dbo].[UnderOverPredicts_Update]

IF OBJECT_ID(N'[dbo].[UnderOverPredicts_Delete]') IS NOT NULL
	DROP PROCEDURE [dbo].[UnderOverPredicts_Delete]

IF OBJECT_ID(N'[dbo].[UnderOverPredicts_Select]') IS NOT NULL
	DROP PROCEDURE [dbo].[UnderOverPredicts_Select]

--endregion

GO


--region [dbo].[UnderOverPredicts_Select]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[UnderOverPredicts_Select]
	@Page int = null,
	@PageSize int = null,
    @SortBy nvarchar(255) = null,
	@SortType bit = null,
	@IdUnderOverPredict int = null,
	@IdMatch int = null,
	@IdUser int = null,
	@Predict int = null
AS

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

Declare @RowCount int  
Set @RowCount = (
					Select Count (*) 
					From [dbo].[UnderOverPredicts]
					WHERE
					(
						(@IdUnderOverPredict is null OR [UnderOverPredicts].[IdUnderOverPredict] = @IdUnderOverPredict)
						AND (@IdMatch is null OR [UnderOverPredicts].[IdMatch] = @IdMatch)
						AND (@IdUser is null OR [UnderOverPredicts].[IdUser] = @IdUser)
						AND (@Predict is null OR [UnderOverPredicts].[Predict] = @Predict)
					)
				)
if(@Page is null)
begin
	Set @Page = 1
end

if(@PageSize is null)
begin
	Set @PageSize = @RowCount
end

if(@SortBy is null)
begin
	Set @SortBy = 'DBNull'
end

if(@SortType is null)
begin
	Set @SortType = 1
end

SELECT *
FROM   (
			SELECT [dbo].[UnderOverPredicts].*,
                   ROW_NUMBER() OVER 
                   (ORDER BY
                        CASE WHEN @SortBy = 'DBNull' THEN NewID() END ASC,
						CASE WHEN @SortBy = 'IdUnderOverPredict' and @SortType = 1 THEN [UnderOverPredicts].[IdUnderOverPredict] END ASC,
						CASE WHEN @SortBy = 'IdUnderOverPredict' and @SortType = 0 THEN [UnderOverPredicts].[IdUnderOverPredict] END DESC,
						CASE WHEN @SortBy = 'IdMatch' and @SortType = 1 THEN [UnderOverPredicts].[IdMatch] END ASC,
						CASE WHEN @SortBy = 'IdMatch' and @SortType = 0 THEN [UnderOverPredicts].[IdMatch] END DESC,
						CASE WHEN @SortBy = 'IdUser' and @SortType = 1 THEN [UnderOverPredicts].[IdUser] END ASC,
						CASE WHEN @SortBy = 'IdUser' and @SortType = 0 THEN [UnderOverPredicts].[IdUser] END DESC,
						CASE WHEN @SortBy = 'Predict' and @SortType = 1 THEN [UnderOverPredicts].[Predict] END ASC,
						CASE WHEN @SortBy = 'Predict' and @SortType = 0 THEN [UnderOverPredicts].[Predict] END DESC                   
                    ) AS RowNumber 
			FROM [dbo].[UnderOverPredicts]
			Where 
			(
				(@IdUnderOverPredict is null OR [UnderOverPredicts].[IdUnderOverPredict] = @IdUnderOverPredict)
				AND (@IdMatch is null OR [UnderOverPredicts].[IdMatch] = @IdMatch)
				AND (@IdUser is null OR [UnderOverPredicts].[IdUser] = @IdUser)
				AND (@Predict is null OR [UnderOverPredicts].[Predict] = @Predict)
			)
		) AS Temp
WHERE  RowNumber Between (@Page * @PageSize - @PageSize + 1) And @Page * @PageSize
Return @RowCount


--end [dbo].[UnderOverPredicts_Select]
--endregion

GO
--=========================================================================================--

	

--region [dbo].[UnderOverPredicts_Insert]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[UnderOverPredicts_Insert]
	@IdUnderOverPredict int OUTPUT,
	@IdMatch int,
	@IdUser int,
	@Predict int

AS


INSERT INTO [dbo].[UnderOverPredicts] 
(
	[IdMatch],
	[IdUser],
	[Predict]
)
VALUES 
(
	@IdMatch,
	@IdUser,
	@Predict
)

SET @IdUnderOverPredict = SCOPE_IDENTITY()

--end [dbo].[UnderOverPredicts_Insert]
--endregion

GO
--=========================================================================================--

--region [dbo].[UnderOverPredicts_Update]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[UnderOverPredicts_Update]
	@IdUnderOverPredict int,
	@IdMatch int,
	@IdUser int,
	@Predict int
AS


UPDATE [dbo].[UnderOverPredicts] SET
	[IdMatch] = @IdMatch,
	[IdUser] = @IdUser,
	[Predict] = @Predict
WHERE
	[IdUnderOverPredict] = @IdUnderOverPredict

--end [dbo].[UnderOverPredicts_Update]
--endregion

GO
--=========================================================================================--

--region [dbo].[UnderOverPredicts_Delete]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[UnderOverPredicts_Delete]
	@IdUnderOverPredict int
AS


DELETE FROM [dbo].[UnderOverPredicts]
WHERE
(
	[IdUnderOverPredict] = @IdUnderOverPredict
)

--end [dbo].[UnderOverPredicts_Delete]
--endregion

GO
--=========================================================================================--

